package com.lgx.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lgx.common.dao.IIntraCityDao;
import com.lgx.common.entity.IntraCity;
import com.lgx.common.exception.DataException;
import com.lgx.common.ip.util.IPSeeker;
import com.lgx.common.service.IIntraCityService;

@Service
public class IntraCityService implements IIntraCityService {

	@Autowired
	private IIntraCityDao intraCityDao;
	
	public IIntraCityDao getIntraCityDao() {
		return intraCityDao;
	}

	public void setIntraCityDao(IIntraCityDao intraCityDao) {
		this.intraCityDao = intraCityDao;
	}

	@Override
	public IntraCity createIntraCity(IntraCity city) throws DataException {
		return this.intraCityDao.createIntraCity(city);
	}

	@Override
	public IntraCity updateIntraCity(IntraCity city) throws DataException {
		return this.intraCityDao.updateIntraCity(city);
	}

	@Override
	public void deleteIntraCity(IntraCity city) throws DataException {
		this.intraCityDao.deleteIntraCity(city);
	}

	@Override
	public void deleteIntraCity(String id) throws DataException {
		this.intraCityDao.deleteIntraCity(id);
	}

	@Override
	public IntraCity findIntraCityById(String id) {
		return this.intraCityDao.findIntraCityById(id);
	}

	@Override
	public List<IntraCity> findIntraCityList() {
		return this.intraCityDao.findIntraCityList();
	}
	
	@Override
	public IntraCity findIntraCityByIP(String ip, String dataName,
			String filePath) {
		String shi = "";
		// 指定纯真数据库的文件名，所在文件夹
		IPSeeker ips = new IPSeeker(dataName, filePath);
		
		String areaName = ips.getIPLocation(ip).getCountry();
		
		if(areaName.contains("省")){
			areaName = areaName.substring(areaName.indexOf("省")+1,areaName.length());
		}else{
			if(areaName.startsWith("香港")){
				shi = "香港";
			}
			if(areaName.startsWith("澳门")){
				shi = "澳门";
			}
			if(areaName.startsWith("广西")){
				areaName = areaName.substring(2,areaName.length());
			}
			if(areaName.startsWith("内蒙古")){
				areaName = areaName.substring(3,areaName.length());
			}
			if(areaName.startsWith("西藏")){
				areaName = areaName.substring(2,areaName.length());
			}
			if(areaName.startsWith("宁夏")){
				areaName = areaName.substring(2,areaName.length());
			}
			if(areaName.startsWith("新疆")){
				areaName = areaName.substring(2,areaName.length());
			}
		}
		if(areaName.contains("州")){
			areaName = areaName.substring(areaName.indexOf("州")+1,areaName.length());
		}
		if(areaName.contains("市")){
			shi = areaName.substring(0, areaName.indexOf("市")+1);
			areaName = areaName.substring(areaName.indexOf("市")+1,areaName.length());
			if(areaName.contains("市")){
				areaName = areaName.substring(areaName.lastIndexOf("市")+1,areaName.length());
			}
		}
		if(areaName.contains("区")){
			areaName = areaName.substring(areaName.indexOf("区")+1,areaName.length());
		}
		if(areaName.contains("县")){
			areaName = areaName.substring(areaName.indexOf("县")+1,areaName.length());
		}
		
		return this.intraCityDao.findIntraCityByAreaName(shi);
	}

}
